package core

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// Success 正确返回
func Success(c *gin.Context, code int, msg string, data interface{}) {
	c.JSON(http.StatusOK, gin.H{
		"err":  code,
		"msg":  msg,
		"data": data,
	})
}

// Error 错误返回
func Error(c *gin.Context, code int, msg string) {
	c.JSON(http.StatusOK, gin.H{
		"err":  code,
		"msg":  msg,
		"data": nil,
	})
}

// ReJson 定义返回结构体
type ReJson struct {
	Code int `json:"code"`
	Msg  any `json:"msg"`
	Data any `json:"data"`
}

// RePaginate 定义分页返回结构体
type RePaginate struct {
	Total       int `json:"total"`
	CurrentPage int `json:"current_page,omitempty"`
	LastPage    int `json:"last_page,omitempty"`
	Data        any `json:"data,omitempty"`
}

// ResOk 成功返回
func ResOk(c *gin.Context, msg string, data interface{}) {
	Json := ReJson{
		Code: 1,
		Msg:  msg,
		Data: data,
	}
	c.JSON(http.StatusOK, Json)
}

// ResErr 错误返回
func ResErr(c *gin.Context, msg string) {
	Json := ReJson{
		Code: 0,
		Msg:  msg,
		Data: nil,
	}
	c.JSON(http.StatusOK, Json)
}

// Paginate 分页返回
func Paginate(c *gin.Context, total, CurrentPage, LastPage int, data interface{}) {
	Page := RePaginate{
		Total:       total,
		CurrentPage: CurrentPage,
		LastPage:    LastPage,
		Data:        data,
	}
	Json := ReJson{
		Code: 1,
		Msg:  "成功",
		Data: Page,
	}
	c.JSON(http.StatusOK, Json)
}
